package com.dwjm.exam.admin;

import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/admin/updateProfile")
public class AdminUpdateProfileHandler extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");

        PrintWriter out = resp.getWriter();
        Map<String, Object> responseMap = new HashMap<>();
        
        try {
            // 获取请求参数
            String adminIdStr = req.getParameter("adminId");
            String realName = req.getParameter("realName");
            String email = req.getParameter("email");
            String phone = req.getParameter("phone");
            String oldPassword = req.getParameter("oldPassword");
            String newPassword = req.getParameter("newPassword");
            
            // 验证管理员ID
            if (adminIdStr == null || adminIdStr.isEmpty()) {
                responseMap.put("code", 400);
                responseMap.put("msg", "缺少管理员ID");
                out.write(new Gson().toJson(responseMap));
                return;
            }
            
            int adminId = Integer.parseInt(adminIdStr);
            
            // 查询管理员信息
            AdminDaoImpl adminDao = new AdminDaoImpl();
            Admin admin = adminDao.getAdminById(adminId);
            
            if (admin == null) {
                responseMap.put("code", 400);
                responseMap.put("msg", "管理员不存在");
                out.write(new Gson().toJson(responseMap));
                return;
            }
            
            // 如果需要修改密码，验证原密码
            if (oldPassword != null && !oldPassword.isEmpty() && newPassword != null && !newPassword.isEmpty()) {
                if (!oldPassword.equals(admin.getPassword())) {
                    responseMap.put("code", 400);
                    responseMap.put("msg", "原密码不正确");
                    out.write(new Gson().toJson(responseMap));
                    return;
                }
                
                // 更新密码
                admin.setPassword(newPassword);
            }
            
            // 更新其他信息
            admin.setRealName(realName);
            admin.setEmail(email);
            admin.setPhone(phone);
            
            // 保存更新
            boolean result = adminDao.updateAdmin(admin);
            
            if (result) {
                responseMap.put("code", 200);
                responseMap.put("msg", "更新成功");
            } else {
                responseMap.put("code", 500);
                responseMap.put("msg", "更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseMap.put("code", 500);
            responseMap.put("msg", "服务器错误: " + e.getMessage());
        }
        
        // 输出响应
        Gson gson = new Gson();
        out.write(gson.toJson(responseMap));
        out.flush();
        out.close();
    }
} 